package com.mobilepay.moneypig.share;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mobilepay.moneypig.R;
import com.mobilepay.moneypig.base.BaseDialog;
import com.mobilepay.moneypig.utils.inject.Inject;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

/**
 * Created by harmy on 2016/8/19 0019.
 */
public class ShareOptionDialog extends BaseDialog implements View.OnClickListener {
    public ShareOptionDialog(Context context) {
        super(context);
    }

    public ShareOptionDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected ShareOptionDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.dialog_shareoption);
    }

    @Override
    protected void initViews() {
        initWindow(1.0f, 0, Gravity.BOTTOM);
        setAnimate(R.style.animatedialog);
    }

    @Override
    protected void onSetListener() {
        wxcircleLL.setOnClickListener(this);
        wxLL.setOnClickListener(this);
        qqLL.setOnClickListener(this);
        qzoneLL.setOnClickListener(this);
        sinaLL.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.wxcircleLL:
                share(SHARE_MEDIA.WEIXIN_CIRCLE);
                break;
            case R.id.wxLL:
                share(SHARE_MEDIA.WEIXIN);
                break;
            case R.id.qqLL:
                share(SHARE_MEDIA.QQ);
                break;
            case R.id.qzoneLL:
                share(SHARE_MEDIA.QZONE);
                break;
            case R.id.sinaLL:
                share(SHARE_MEDIA.SINA);
                break;
            case R.id.tv_cancel:
                dismiss();
                break;
        }
        cancel();
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {

    }

    @Inject(R.id.wxcircleLL)
    private LinearLayout wxcircleLL;
    @Inject(R.id.wxLL)
    private LinearLayout wxLL;
    @Inject(R.id.qqLL)
    private LinearLayout qqLL;
    @Inject(R.id.qzoneLL)
    private LinearLayout qzoneLL;
    @Inject(R.id.sinaLL)
    private LinearLayout sinaLL;
    @Inject(R.id.tv_cancel)
    private TextView tv_cancel;

    private UMImage image;
    private String text;
    private String title;
    private String target;
    private UMShareListener listener;

    public void setText(String text) {
        this.text = text;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public void setListener(UMShareListener listener) {
        this.listener = listener;
    }

    public void setImage(UMImage image) {
        this.image = image;
    }

    private void share(SHARE_MEDIA platform) {
        if (UMShareAPI.get(getActivity()).isInstall(getActivity(), platform)) {
            ShareAction shareAction = new ShareAction(getActivity());
            shareAction
                    .setPlatform(platform)
                    .withMedia(image)
                    .withText(text);
            if (listener != null) {
                shareAction.setCallback(listener);
            }
            shareAction.share();
        } else {
//            switch (platform) {
//                case WEIXIN:
//                    showToastShort("请先安装微信客户端");
//                    break;
//                case WEIXIN_CIRCLE:
//                    showToastShort("请先安装微信客户端");
//                    break;
//                case QQ:
//                    showToastShort("请先安装手机QQ客户端");
//                    break;
//                case QZONE:
//                    showToastShort("请先安装手机QQ客户端");
//                    break;
//                case SINA:
//                    showToastShort("请先安装新浪微博客户端");
//                    break;
//            }
        }
    }
}
